#include "SPI.h"



/************************************************************************************
*************************************************************************************
*                                    Functions                                      *
*************************************************************************************
************************************************************************************/

/*********************************************************
* Name: SPI_Init for the 1st SPI channel 
*
* Desc: Initialize SPI2 Module
*
* Parameter: None
*
* Return: None
*             
**********************************************************/
void SPI_Init(void)
{
   
  SPI_SS = 1;
  _SPI_SS= 1;
  
  SPI1BR = 0x16;              // 93KHz SPI clock    		
  SPI1C2 = 0x00;     
  SPI1C1 = 0x50;  //SPI1C1 = 0b01010000  turn on spi, master mode, 
}

void SPI_Init2(void)
{
   
  SPI_SS2 = 1;
  _SPI_SS2= 1;
  
  SPI2BR = 0x16;              // 93KHz SPI clock    		
  SPI2C2 = 0x00;     
  SPI2C1 = 0x50;  //SPI1C1 = 0b01010000  turn on spi, master mode, 
}

/*********************************************************
* Name: SPI_Send_byte  for the 1st SPI channel
*
* Desc: Send one byte 
*
* Parameter: The byte to be sent
*
* Return: None
*             
**********************************************************/
void SPI_Send_byte(byte u8Data)
{
  char i;
	while(!SPI1S_SPTEF);
	(void)SPI1S;
	SPI1DL=u8Data;
	while(!SPI1S_SPRF);
	(void)SPI1DL;


}

void SPI_Send_byte2(byte u8Data)
{
  char i;
	while(!SPI2S_SPTEF);
	(void)SPI2S;
	SPI2DL=u8Data;
	while(!SPI2S_SPRF);
	(void)SPI2DL;


}

/*********************************************************
* Name: SPI_Receive_byte  for the 1st SPI channel
*
* Desc: The byte received by SPI  
*
* Parameter: None
*
* Return: Received byte
*             
**********************************************************/
byte SPI_Receive_byte(void)
{
	SPI1DL=0x00;
	while(!SPI1S_SPRF);	
		
	return(SPI1DL);
}

 byte SPI_Receive_byte2(void)
{
	SPI2DL=0x00;
	while(!SPI2S_SPRF);	
		
	return(SPI2DL);
}


/*********************************************************
* Name: SPI_High_rate for the 1st SPI channel
*
* Desc: Change SPI baud rate to high speed
*
* Parameter: None
*
* Return: None
*             
**********************************************************/
void SPI_High_rate(void)
{
  SPI1C1 = 0x00;
  SPI1BR = 0x10;          // 6MHz SPI clock    		
  SPI1C1 = SPI1C1_SPE_MASK | SPI1C1_MSTR_MASK;

}



